import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// import axios from 'axios'
import moment from 'moment/moment'
import Vant from "vant"
import { Toast, Lazyload } from "vant"
import 'vant/lib/index.css'
import 'vant/lib/index.less';
import axios from './http/http.js'
import VueAxios from "vue-axios"
// import { set } from 'core-js/core/dict'

Vue.use(Vant)
Vue.use(Toast)
Vue.use(Lazyload)
Vue.use(VueAxios, axios)


Vue.prototype.$moment = moment
Vue.prototype.$axios = axios
Vue.prototype.$toast = Toast
// 将api.js挂载到全局
Vue.prototype.$api = require("@/http/api.js")




// 配置axios响应拦截器Response
axios.interceptors.response.use(res => {
  // 当token过期或不存在,后台返回状态码202,此时页面跳转到登录
  // if (res.status === 401) {
  //   Toast.fail("登录信息已过期,重新登录!")
  //   router.replace("/login")
  // }
  if (res?.status === 401 || (res?.status === 200 && res?.data.code === 202)) {
    Toast.fail("登录信息已过期,请重新登录!")
    router.replace({ path: "/login" })
  }
  return res
}, err => {
  return Promise.reject(err)
})

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

// 移动端建议:使用rem作为像素单位
// 设置rem以iphone7为标准,宽度375px 设置1rem=10px
const setFontSize = () => {
  const hmtlDom = document.getElementsByTagName('html')[0]
  let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
  if (htmlWidth >= 750) {
    htmlWidth = 750
  }
  if (htmlWidth <= 320) {
    htmlWidth = 230
  }
  hmtlDom.style.fontSize = `${htmlWidth / 37.5}px`
}
window.onresize = setFontSize()
setFontSize()